package controllers.listeners.feature;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import controllers.shared.GetInputLoop;

import objects.Feature;
import views.ViewFeatures;

/* Edits the selected feature's name or value */
/* actionPerformed when you click on an item in the table */
public class FeatureModuleMouseListener implements MouseListener {
	
	private ViewFeatures view;
	
	public FeatureModuleMouseListener() {
		view = null;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		int clickCount = e.getClickCount();
		JTable table = (JTable) e.getSource();
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		
		int row = table.getSelectedRow();
		int column = table.getSelectedColumn();
		
		if (clickCount < 2) {
			return;
		}
		
		if (tableModel.getValueAt(row, tableModel.findColumn("Editable")).equals("No")) {
			return;
		}
		
		if (!(column == tableModel.findColumn("Name") || column == tableModel.findColumn("Value"))) {
			return;
		}
		
		Feature feature = null;
		String featureName = (String) tableModel.getValueAt(row, tableModel.findColumn("Name"));
		
		if(view.getSelectedFeatureType().equals("Semantic Rep")) {
			for (Feature currentFeature : view.getCurrentConstituent().getFeatures().getFeatureList()) {
				if (currentFeature.getName().equals(featureName)) {
					feature = currentFeature;
					break;
				}
			}
		} else if(view.getSelectedFeatureType().equals("Lexicon")) {
			for (Feature currentFeature : view.getCurrentLexicon().getFeatures().getFeatureList()) {
				if (currentFeature.getName().equals(featureName)) {
					feature = currentFeature;
					break;
				}
			}
		}
		
		if (column == tableModel.findColumn("Name")) {
			String replacement = null;
			
			/* Get feature value */
			GetInputLoop.setInputDialog("Enter the new name of the feature '" + featureName + "'.", "Edit Feature", JOptionPane.OK_CANCEL_OPTION);
			GetInputLoop.setErrorDialog("Please enter a name.", "Error");
			replacement = GetInputLoop.getInput();
			
			if (replacement == null) {
				return;
			}
			
			feature.setName(replacement);
		}
		else if (column == tableModel.findColumn("Value")) {
			String value = (String) tableModel.getValueAt(row, tableModel.findColumn("Value"));
			
			ArrayList<String> featureValues = feature.getValueList();
			int index = featureValues.indexOf(value);
			
			String replacement = null;
			
			/* Get feature value */
			GetInputLoop.setInputDialog("Enter the new name of the value '" + value + "' for the feature '" + featureName + "'.", "Edit Value", JOptionPane.OK_CANCEL_OPTION);
			GetInputLoop.setErrorDialog("Please enter a value.", "Error");
			replacement = GetInputLoop.getInput();
			
			if (replacement == null) {
				return;
			}
			
			featureValues.remove(index);
			featureValues.add(index, replacement);
		}
		
		if(view.getSelectedFeatureType().equals("Semantic Rep")) {
			view.loadConstituent(view.getCurrentConstituent());
		}
		else if(view.getSelectedFeatureType().equals("Lexicon")) {
			view.loadLexicon(view.getCurrentLexicon());
		}
	}
	
	@Override
	public void mouseEntered(MouseEvent e) {}
	
	@Override
	public void mouseExited(MouseEvent e) {}
	
	@Override
	public void mousePressed(MouseEvent e) {}
	
	@Override
	public void mouseReleased(MouseEvent e) {}
	
	public void setView(ViewFeatures view) {
		this.view = view;
	}
}
